package com.kc.lib_base.utils;

import android.content.res.Resources;

/**
 * @author created by kingchao
 * @ClassName SizeUtils
 * @时间：2021/8/25 9:28
 * @note:
 */
public class SizeUtils {

    /**
     * 在一个可用的区域内，计算出一个比例为ratio 的最大的宽高
     *
     * @param ratio
     * @param maxWidth
     * @param maxHeight
     * @return
     */
    public static int[] getMaxSize(float ratio, int maxWidth, int maxHeight) {
        int width = 0;
        int height = 0;
        for (int i = 0; i < 20; i++) {
            width = (int) (maxWidth * (1 - i * 0.05));
            height = (int) (width * ratio);
            if (height < maxHeight) {
                break;
            }
        }
        return new int[]{width, height};
    }

    /**
     * dp to px
     *
     * @param dpValue
     * @return
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dp(final float pxValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
